import axios from "axios";
import { defineStore } from "pinia";
import { parse } from "vue/compiler-sfc";
import { reactive } from "vue";

// export const useLoveTolkStore = defineStore('loveTalk', ({ 
//     actions: {
//         async getTalka() {
//             const { data: { id: id }, data: { hitokoto: content } } = await axios.get('https://v1.hitokoto.cn/')
//             this.loveTalkList.push({ id, content })
//         }
//     },
//     state() {
//         return {
//             loveTalkList: JSON.parse(localStorage.getItem('talkList') as string) || [{id:1,content:'3213213'}]
//         }
//     }
// }))



//store 组合式写法
export const useLoveTolkStore = defineStore('loveTalk', () => {

    //相当于state
    const loveTalkList = reactive(
    JSON.parse(localStorage.getItem('talkList') as string) || [{ id: 1, content: '3213213' }]
)

    async function getTalka() {
        const { data: { id: id }, data: { hitokoto: content } } = await axios.get('https://v1.hitokoto.cn/')
        loveTalkList.push({ id, content })
    }
    return {loveTalkList, getTalka}
})